package control;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import model.SpeciesFeatures;
import interfaces.ISpeciesFeatures;

public class CSpeciesFeatures implements ISpeciesFeatures {
  private CConnection db;

  public CSpeciesFeatures(CConnection db) {
	this.db = db;
  }
	
  @Override
  public boolean create(SpeciesFeatures sf) throws SQLException, ClassNotFoundException {
    Statement s = db.getStatement();
    int i = s.executeUpdate("INSERT INTO Species_Features VALUES (" + sf.getFeatureID() + ", '" + sf.getSpeciesName() + "')");
    return i > 0;
  }

  @Override
  public boolean update(SpeciesFeatures sf) throws SQLException, ClassNotFoundException {
    return false;
  }
  
  @Override
  public boolean delete(SpeciesFeatures sf) throws SQLException, ClassNotFoundException {
    Statement s = db.getStatement();
    int i = s.executeUpdate("DELETE FROM Species_Features WHERE FeatureID = " + sf.getFeatureID() + " AND Specie = '" + sf.getSpeciesName() + "'");
    return i > 0;
  }

  @Override
  public ArrayList<SpeciesFeatures> retrieveAll() throws SQLException, ClassNotFoundException {
    Statement s = db.getStatement();
    ResultSet res = s.executeQuery("select * from species_features");
		
    ArrayList<SpeciesFeatures> comb = new ArrayList<SpeciesFeatures>();
    
    while(res.next()) {
      SpeciesFeatures sf = new SpeciesFeatures();
      sf.setFeatureID(res.getInt("FeatureID"));
      sf.setSpeciesName(res.getString("Specie"));
      comb.add(sf);
    }
    
    return comb;
  }
}
